package com.sjjy.ta.action;

import com.sjjy.ta.form.MailInfoForm;
import com.sjjy.ta.iface.MailInfoService;
import com.sjjy.ta.iface.MonitoringService;
import com.sys.plat.util.HttpUtil;
import com.sys.plat.web.core.ActionContext;
import com.sys.plat.web.core.BaseAction;
import org.springframework.web.context.support.WebApplicationContextUtils;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by IntelliJ IDEA.
 * User: mabo
 * Date: 11-3-11
 * Time: 下午8:21
 * To change this template use File | Settings | File Templates.
 */
public class MailInfoAction extends BaseAction {


    private MailInfoForm getForm() {
        return (MailInfoForm) ActionContext.getForm();
    }

    private MailInfoService getService() {
        return (MailInfoService) getService("MailInfoImpl");
    }


    public String pastMail() {
        HttpUtil.reqSetAttr("list", getService().mailList(getForm().getUid()));
        HttpUtil.reqSetAttr("uid", getForm().getUid());
        return "pastMail";
    }


    public String openPostPage() {
        HttpUtil.reqSetAttr("data", getForm().getData());
        HttpUtil.reqSetAttr("uid", getForm().getUid());
        return "postMail";
    }

    public String postMail() {


        //群发
        if (getForm().getData("mailList") != null && !getForm().getData("mailList").equals("")) {
            String[] strs = getForm().getData("mailList").split("\r\n");
            Map doc = new HashMap() {{
                put("doc", "你好，可以聊聊吗？告诉我QQ号，咱们QQ联系也行^_^（邮票附送啦~~~）");
            }};

            List list = new ArrayList();

            for (int i = 0; i < strs.length; i++) {
                String str = strs[i];
                if (str == null || str.equals("")) {
                    continue;
                }
                strs[i] = str.replace("http://www.jiayuan.com/", "");
            }

            list = getService().postMail(strs, doc);

            /*MonitoringService m = (MonitoringService) getService("MonitoringImpl");
            m.login();
            m.saveOutMail();*/

            if (list.size() > 0) {
                try {
                    PrintWriter pw = ActionContext.getResponse().getWriter();
                    for (Object o : list) {
                        pw.print("<span style=\"color:red;\">" + o.toString() + "</span><br>");
                    }
                } catch (IOException e) {
                    e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
                }
            } else {
                return "close";
            }
        } else {
            int i = 2;
            if (getForm().getData("swift") != null && getForm().getData("swift").equals("1")) {
                getForm().getData().put("doc", "你好，可以聊聊吗？告诉我QQ号，咱们QQ联系也行^_^（邮票附送啦~~~）");
            }
            i = getService().postMail(getForm().getUid().trim(), getForm().getData());

            if (i == 1) {
                try {
                    PrintWriter pw = ActionContext.getResponse().getWriter();
                    pw.print("<script language=\"javascript\">\n" +
                            "  window.close();\n" +
                            "</script>");
                } catch (IOException e) {
                    e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
                }
                return "close";
            } else if (i == 2) {
                try {
                    PrintWriter pw = ActionContext.getResponse().getWriter();
                    pw.print("<span style=\"color:red;\">信件发送失败！！</span>");
                } catch (IOException e) {
                    e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
                }
            } else if (i == 3) {
                try {
                    PrintWriter pw = ActionContext.getResponse().getWriter();
                    pw.print("<span style=\"color:red;\">已经给此用户发送过邮件！！</span>");
                } catch (IOException e) {
                    e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
                }
            }
        }


        return "";
    }
}
